#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWidget>
#include <QVBoxLayout>
#include <QMenuBar>
#include <QLineEdit>

#include "systemcontrol.h"

class QLineEdit;
class QPushButton;

class SystemControl;

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget * parent = 0,
               Qt::WindowFlags flags = 0,
               SystemControl *system = 0);

    void init();

    QString getOctavePath();
    int     getOctaveResolution();

    class MainWidget;
    class Toolkit;
    class ButtonGroup;
    class MenuBar;
    class OptionPane;

public slots:
    void newScenario();
    void openScenario();
    void openScenario(const QString &fileName);
    void saveScenario();

    void optionWindow();

    void aboutWindow();

protected:
    void connectMenuBar();
    void updateRecentFiles();
    void writeRecentFiles();
    //void updateInputValues();

    QStringList m_recent_files;

    MainWidget *m_mainWidget;
    MenuBar *m_menubar;
    OptionPane *m_options;
    SystemControl *m_system;
};


class MainWindow::MainWidget : public QWidget
{
    Q_OBJECT
public:
    friend class MainWindow;

    MainWidget(QWidget *parent = 0, SystemControl *system = 0);

    inline Toolkit* getToolkit() {return toolkit;}

signals:
    void outputChanged(QString);

public slots:
    /** Functions that launch some kind of window. */
    void showEditor();
    void showMatrix();
    void showSurface();

    /** Functions that can edit the system */
    void setInputOneFuzzy(QString);
    void setInputTwoFuzzy(QString);

private:

    Toolkit* toolkit;
    SystemControl *m_system;

    void checkOutput();
};

class MainWindow::Toolkit : public QVBoxLayout
{
    Q_OBJECT

    friend class MainWindow;

public:
    Toolkit();

public slots:

    void resetInputOneFuzzy();
    void resetInputTwoFuzzy();

protected:
    QLineEdit *inputA;
    QLineEdit *inputB;
    QLineEdit *output;
};

class MainWindow::ButtonGroup : public QVBoxLayout
{
    friend class MainWindow;

public:
    ButtonGroup();

protected:
    QPushButton *load;
    QPushButton *save;
    QPushButton *surface;
    QPushButton *editor;
    QPushButton *matrix;
    QPushButton *quit;
};

class MainWindow::MenuBar : public QMenuBar
{
    friend class MainWindow;

public:
    MenuBar(QWidget *parent = 0);
    void updateRecentFiles(const QStringList &files);

private:

    QMenu *file;
    QAction *create;
    QAction *open;
    QAction *save;
    QVector<QAction *> files;

    QMenu *edit;
    QAction *reset;
    QAction *options;

    QMenu *help;
    QAction *about;
};

class MainWindow::OptionPane : public QWidget
{
    Q_OBJECT

public:
    OptionPane(QWidget *parent = 0);

    inline QString getOctavePath() const
                { return octavePath->text(); }
    inline int getOctaveResolution() const
                { return octaveResolution->text().toInt(); }

public slots:
    void findOctavePath();
    void rewriteOptionsFile();

private:

    QLineEdit *octavePath;
    QLineEdit *octaveResolution;
};

#endif // MAINWINDOW_H
